package com.sanhe.roller.util;

import android.app.ActivityManager;
import android.app.backup.BackupManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.finddreams.languagelib.LanguageType;
import com.sanhe.roller.App;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;

public class Utils {

    public static int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) App.getApp()
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getMetrics(dm);
        return dm.widthPixels;
    }

    public static int getScreenHeight() {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) App.getApp()
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getMetrics(dm);
        return dm.heightPixels;
    }

    public static int dp2Px(float dipValue) {
        final float scale = App.getApp().getResources()
                .getDisplayMetrics().density;
        int pxValue = (int) (dipValue * scale + 0.5f);

        return pxValue;
    }

    public static void changeSysLan(LanguageType type) {
//        Locale locale = Locale.ENGLISH;//英文
        Locale locale = new Locale(type.str());
        if (Build.VERSION.SDK_INT >= 26) {
            updateLocales(new LocaleList(locale));
            return;
        }
        Class amnClass = null;
        try {
            amnClass = Class.forName("android.app.ActivityManagerNative");
            Object amn = null;
            Configuration config = null;

            // amn = ActivityManagerNative.getDefault();
            Method methodGetDefault = amnClass.getMethod("getDefault");
            methodGetDefault.setAccessible(true);
            amn = methodGetDefault.invoke(amnClass);

            // config = amn.getConfiguration();
            Method methodGetConfiguration = amnClass.getMethod("getConfiguration");
            methodGetConfiguration.setAccessible(true);
            config = (Configuration) methodGetConfiguration.invoke(amn);

            // config.userSetLocale = true;
            Class configClass = config.getClass();
            Field f = configClass.getField("userSetLocale");
            f.setBoolean(config, true);

            // set the locale to the new value
            config.locale = locale;

            // amn.updateConfiguration(config);
            Method methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
            methodUpdateConfiguration.setAccessible(true);
            methodUpdateConfiguration.invoke(amn, config);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void updateLocales(LocaleList locales) {
        try {
            Method m = ActivityManager.class.getDeclaredMethod("getService");
            m.setAccessible(true);
            Object am = m.invoke(null);
            Configuration config = (Configuration) invoke(am, "getConfiguration");

//            final IActivityManager am = ActivityManager.getService();
//            final Configuration config = am.getConfiguration();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocales(locales);
            }
            Field f = config.getClass().getDeclaredField("userSetLocale");
            f.setAccessible(true);
            f.set(config, true);
//            config.userSetLocale = true;
            invoke(am, "updatePersistentConfiguration", config);
//            am.updatePersistentConfiguration(config);
            // Trigger the dirty bit for the Settings Provider.
            BackupManager.dataChanged("com.android.providers.settings");
        } catch (Throwable e) {
            // Intentionally left blank
            e.printStackTrace();
        }
    }

    private static Object invoke(Object obj, String name, Object param) {
        try {
            Class clazz = obj.getClass();
            Method m;
            while (true) {
                m = clazz.getDeclaredMethod(name, param.getClass());
                if (m != null) {
                    m.setAccessible(true);
                    break;
                } else {
                    clazz = clazz.getSuperclass();
                    if (clazz == null) {
                        break;
                    }
                }
            }
            if (m != null) {
                return m.invoke(obj, param);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Object invoke(Object obj, String name) {
        try {
            Class clazz = obj.getClass();
            Method m;
            while (true) {
                m = clazz.getDeclaredMethod(name);
                if (m != null) {
                    m.setAccessible(true);
                    break;
                } else {
                    clazz = clazz.getSuperclass();
                    if (clazz == null) {
                        break;
                    }
                }
            }
            if (m != null) {
                return m.invoke(obj);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
}
